class GameagentView extends egret.DisplayObjectContainer{
	public static GAMEAGENT_VIEW:string="gameagentview";//设置的切换场景

	private backdropGame = new BackdropGame();//背景

	private homeGame = new HomeGame();//僵尸

	private peopleGame = new PeopleGame();//人物

	private sideGame = new SideGame();//贴符

	private textGame = new TextGame();//分数

	private darknessView = new BackgroundConfig();//遮盖层

	private clickView = new ClickBut();//按钮层

	private ButClick:egret.Bitmap;//点击按钮

	private leftNume = [150,400];//左边位置

	private rightNUm = [210,400];//右边位置

	private coloNum = false;//判断是否在正确范围内可以点击

	private isClick=true;//用来结束时取消点击

	private antNume=true;//道士的动画判断

	private isOK=true; //成功或失败

	public addSpeed:egret.Timer = new egret.Timer(8000,0);//定时器创建房子

	private sppp=4; //动画移动的速度

	public constructor() {
		super();
	};

	//初始化素材信息 
	public init(){
		// 添加背景
		this.addChild(this.backdropGame);
		this.backdropGame.start();

		// 添加僵尸和僵尸的动画
		// 添加房子 并启动创建房子和移动的定时器
		this.addChild(this.homeGame);
		this.homeGame.starTime();
		this.homeGame.HoustTime();

		// 添加人物
		this.addChild(this.peopleGame);

		// 添加贴符
		this.addChild(this.sideGame);

		//添加分数
		this.addChild(this.textGame);
		
		// 初始化点击按钮
		this.addEventListener(egret.Event.ADDED_TO_STAGE,this.initClick,this);

		// 开启监听
		this.startEnt();

		// 加速度
		this.HoustTime();
	};

	// 点击按钮
	private initClick() {
		this.ButClick= new egret.Bitmap(); 
	
		this.ButClick.texture = RES.getRes("up_png");
		
		this.ButClick.width = this.ButClick.texture.textureWidth / 2;
		
		this.ButClick.height = this.ButClick.texture.textureHeight / 2;
		
		this.ButClick.x = (this.stage.stageWidth - this.ButClick.width) / 2 ;
		
		this.ButClick.y= this.stage.stageHeight- this.ButClick.height * 2;
		
		this.addChild(this.ButClick);
		
		this.ButClick.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
			this.ButClick.texture = RES.getRes("down_png");
		},this);//鼠标按下
		
		this.ButClick.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
			this.ButClick.texture = RES.getRes("up_png");
		},this);//鼠标抬起
		
		this.ButClick.touchEnabled=true;
		
		this.ButClick.addEventListener(egret.TouchEvent.TOUCH_TAP,this.clickFun,this);
	};

	// 帧动画监视没有操作时候的判断
	private startEnt() {
		this.addEventListener(egret.Event.ENTER_FRAME,this.addEvent,this);
	};

	// 监听信息 -- 如果僵尸Y2到达人的Y的时候提示条变成绿色
	private addEvent(){
		if(this.homeGame.HomeArr.length > 0 ){
			let J1 = this.homeGame.HomeArr[0].home.y;//僵尸的Y轴用来判断是否达到道士的顶部

			let J2 = this.homeGame.HomeArr[0].home.y + this.homeGame.HomeArr[0].home.height;//僵尸的Y2轴来判断是否达到道士的顶部

			let P1 = this.peopleGame.people.y; //道士的Y

			let P2 = this.peopleGame.people.y + this.peopleGame.people.height;//道士的Y2

			// 僵尸的J2位置达到人的顶部P1 提示条变成绿色
			if(J2 >= P1 && J1 < P1){
				this.homeGame.HintArr[0].texture = this.homeGame.Tooltip;
				
				this.coloNum = true;//这个区间点击是正确的
			}else if(J1 > P2){
				this.homeGame.HintArr[0].texture = this.homeGame.TooltipShade;
				
				this.coloNum = false;//这个区间点击是正确的
			}
			// 游戏死亡  第一个僵尸的属性为假   僵尸的顶部大于人的顶部
			if(this.homeGame.HomeArr[0].isTrue == false && J1 >= P1 ){
				this.end();
			};
		}
	};

	public HoustTime() {
	
		this.addSpeed.start();
		this.addSpeed.addEventListener(egret.TimerEvent.TIMER,this.addSpends,this);
	};
	// 加速度函数
	private addSpends() {
		this.sppp++;
		this.homeGame.speed=this.sppp;
		this.backdropGame.imgAdd=this.sppp;

		if(this.sppp == 7){
			this.addSpeed.stop();
		}
	}

	// 点击逻辑
	private clickFun(){
		if(this.isClick){
			if(this.coloNum){
				this.zhengque();
			}else{
				this.cuowu();
			};
			this.peopleGame.people.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
						// 恢复骑车动画
						if(this.antNume==true){

							this.peopleGame.people.gotoAndPlay("move",-1);
						}
			}, this);
		}
	};

	// 正确
	private zhengque(){
		this.homeGame.HomeArr[0].isTrue = true;
		
		this.isOK=true;

		// 判断左右
		switch(this.homeGame.HomeArr[0].objX){
			case 0:
				this.peopleGame.people.gotoAndPlay("left",1);//人物动画 --左
				setTimeout( ()=>{
					this.animtLeft();
					
					this.textGame.textTitle.visible=true;
					
					this.textGame.textTitle.texture=this.textGame.preImg;
					
					this.ScoreText();	
				},300);
				break;
			case 1:
				this.peopleGame.people.gotoAndPlay("right",1);//人物动画 --右
				setTimeout( ()=>{
					this.animtRight();
					
					this.textGame.textTitle.visible=true;
					
					this.textGame.textTitle.texture=this.textGame.preImg;
					
					this.ScoreText();	
				},300);
				break;	
			default:
				break;	
		};
		
	};

	// 正确动画
	private animtLeft() {
		this.sideGame.symbol.x=this.leftNume[0];
		
		this.sideGame.symbol.y=this.leftNume[1];
		
		this.sideGame.symbol.visible=true;
		
		let x;
		
		let y;

		x=this.homeGame.HomeArr[0].home.x - this.homeGame.HomeArr[0].home.width / 2 ;
		
		y=this.homeGame.HomeArr[0].home.y + 15 ;
		
		// 执行抛物线传入参数 物体：外卖盒子 X轴 Y轴
		this.parabolaLeft(this.sideGame.symbol,x,y+this.homeGame.speed*(60*2 / 10));
		
		setTimeout(()=>{
			this.culleAntmin();
		},60*4);
	};

	private animtRight() {
		this.sideGame.symbol.x=this.rightNUm[0];
		
		this.sideGame.symbol.y=this.rightNUm[1];
		
		this.sideGame.symbol.visible=true;
		
		let x;
		
		let y;

		x=this.homeGame.HomeArr[0].home.x + this.homeGame.HomeArr[0].home.width / 2 ;
		
		y=this.homeGame.HomeArr[0].home.y + 15 ;
		
		// 执行抛物线传入参数 物体：外卖盒子 X轴 Y轴
		this.parabolaRight(this.sideGame.symbol,x,y+this.homeGame.speed*(60*2 / 10));
		
		setTimeout(()=>{
			this.culleAntmin();
		},60*4);
	};
	// 正确执行完之后，到达位置玻璃动画出现然后切换房子
	private culleAntmin() {
		// 外卖消失
		this.sideGame.symbol.visible=false;
		
		if(this.homeGame.HomeArr.length==0){
			// console.log(1)
		}else{
			if(this.isOK){
				this.homeGame.HomeArr[0].home.gotoAndPlay("dead",1);
			}else{
				this.homeGame.HomeArr[0].home.gotoAndPlay("shengli",1);
				this.end();
			}
		}

	};

	// 错误
	private cuowu() {
		this.isOK=false;
		// 判断左右
		switch(this.homeGame.HomeArr[0].objX){
			case 0:
				this.peopleGame.people.gotoAndPlay("left",1);//人物动画 --左
				setTimeout( ()=>{
						this.errLeft();
				},300);
				break;
			case 1:
				this.peopleGame.people.gotoAndPlay("right",1);//人物动画 --右
				setTimeout( ()=>{
						this.errRight();
				},300);
				break;	
			default:
				break;	
		};
	};

	// 错误动画
	private errLeft() {
		this.sideGame.symbol.x=this.leftNume[0];
		
		this.sideGame.symbol.y=this.leftNume[1];
		
		this.sideGame.symbol.visible=true;
		
		let x;
		
		let y;

		x=this.homeGame.HomeArr[0].home.x - this.homeGame.HomeArr[0].home.width / 2 ;
		
		y=this.homeGame.HomeArr[0].home.y + 50 ;
		
		// 执行抛物线传入参数 物体：外卖盒子 X轴 Y轴
		this.parabolaLeft(this.sideGame.symbol,x,y+this.homeGame.speed*(60*2 / 10));
		
		setTimeout(()=>{
			this.culleAntmin();
		},60*4);
	};

	private errRight() {
		this.sideGame.symbol.x=this.rightNUm[0];
		
		this.sideGame.symbol.y=this.rightNUm[1];
		
		this.sideGame.symbol.visible=true;
		
		let x;
		
		let y;

		x=this.homeGame.HomeArr[0].home.x + this.homeGame.HomeArr[0].home.width / 2 ;
		
		y=this.homeGame.HomeArr[0].home.y + 50 ;
		
		// 执行抛物线传入参数 物体：外卖盒子 X轴 Y轴
		this.parabolaRight(this.sideGame.symbol,x,y+this.homeGame.speed*(60*2 / 10));
		
		setTimeout(()=>{
			this.culleAntmin();
		},60*4);
	};


	// 左边抛物线
	private parabolaLeft(e,a,b){
		var tws = egret.Tween.get(e);
		tws.to({x:110,y:380},60,egret.Ease.backInOut)
		   .to({x:100,y:350},60,egret.Ease.backInOut)
		   .to({x:70,y:380},60,egret.Ease.backInOut)
		   .to({x:a,y:b},60,egret.Ease.backInOut);
	};
	private parabolaRight(e,a,b) {
		var tws = egret.Tween.get(e);
		tws.to({x:250,y:380},60,egret.Ease.backInOut)
		.to({x:280,y:350},60,egret.Ease.backInOut)
		.to({x:320,y:380},60,egret.Ease.backInOut)
		.to({x:a,y:b},60,egret.Ease.backInOut);
	
	};
	// 加分数
	private ScoreText() {
		this.textGame.addNum(3);
		// 提示消失掉 
		setTimeout(()=>{this.noneTitle();},800); 
	};

	// 提示内容消失
	private noneTitle() {
		this.textGame.textTitle.visible = false;
	}

	// 结束 错误的提示 isClick为false不能点击  antNume为false 道士动画停止 背景动画移除停止移动  僵尸动画停止移动 监听移除  
	// 出现遮盖层 出现再来一次按钮/获取次数按钮   分数 排行榜 分享
	private end() {
		// 结束时显示错误提示
		this.textGame.textTitle.visible=true;
		this.textGame.textTitle.texture=this.textGame.endImg;

		// 不能点击
		this.isClick=false;

		// 动画停止
		this.backdropGame.end();
		this.homeGame.end();
		
		// 人物失败
		this.antNume=false;
		this.peopleGame.people.gotoAndPlay("move",0);

		//结束监控
		this.removeEventListener(egret.Event.ENTER_FRAME,this.addEvent,this);

		//显示遮盖层 和按钮层
		this.addChild(this.darknessView);
		this.addChild(this.clickView);
		
		this.endValue();
		
	};

	// 结束分数显示
	private endValue(){
		// 字符串长度
		let a = this.textGame.textNum.text.length;
		// 图片路径
		let b = "phb_";
		let c = "_png";
		let d = this.textGame.textNum.text
		if(a == 1){
			this.clickView.four.texture = RES.getRes(b+d[0]+c);
		}else if(a == 2){
			this.clickView.three.texture = RES.getRes(b+d[0]+c);
			this.clickView.four.texture = RES.getRes(b+d[1]+c);
		}else if(a == 3){
			this.clickView.two.texture = RES.getRes(b+d[0]+c);
			this.clickView.three.texture = RES.getRes(b+d[1]+c);
			this.clickView.four.texture = RES.getRes(b+d[2]+c);
		}else if(a == 4){
			this.clickView.one.texture = RES.getRes(b+d[0]+c);
			this.clickView.two.texture = RES.getRes(b+d[1]+c);
			this.clickView.three.texture = RES.getRes(b+d[2]+c);
			this.clickView.four.texture = RES.getRes(b+d[3]+c);
		}


		var request: egret.HttpRequest;
        request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.addEventListener(egret.Event.COMPLETE,this.onPostComplete,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);
		request.open("http://minijiangshi.icaoyao.vip:8082/miniGames/updateScore" , egret.HttpMethod.POST);
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send("gameName=daoshi&wid="+localStorage.getItem('openid')+"&nick="+localStorage.getItem('userInfo')+"&score="+d);
	};
	
	private onPostComplete(e:egret.Event):void{
		var request = <egret.HttpRequest>e.currentTarget;
		console.log(request.response); 
		// localStorage.setItem("ciNum",request.response);
		this.clickView.endGame(request.response);

	} 

	private onPostIOError(e:egret.IOErrorEvent):void{

	}
}